package com.bainarm.testteabarmachine;

import com.bainarm.library_http_api.HttpApi;
import androidx.appcompat.app.AppCompatActivity;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.io.IOException;

public class HomeActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        // 返回首页按钮
        TextView btnExit = findViewById(R.id.btnExit);
        btnExit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                // 返回到首页的逻辑，例如
                Intent intent = new Intent(HomeActivity.this, MainActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
                // TODO 清理登录记录
                finish();  // 结束当前活动
            }
        });
        // 开户单项测试按钮
        Button btnSingleTest = findViewById(R.id.btnSingleTest);
        btnSingleTest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 开启单项测试

                // TODO 获取远程API数据
                initRequest();
            }
        });
    }


    private void initRequest() {
        ProgressDialog progressDialog = new ProgressDialog(HomeActivity.this);
        progressDialog.setMessage("Loading..."); // 设置加载消息
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); // 设置样式为圆圈进度条
        progressDialog.setCancelable(false); // 设置对话框是否可以通过点击取消

        progressDialog.show(); // 显示进度条
        String url = "https://worldtimeapi.org/api/timezone/Asia/Shanghai";
        HttpApi.getRequest(url, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        progressDialog.dismiss(); // 隐藏进度条
                        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE); // 恢复屏幕操作
                        handleFailure(e.getMessage()); // UI更新操作
                    }
                });
                Log.e("leo", "onFailure: " + e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                final String responseData = response.body().string(); // 读取响应数据
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
//                        handleResponse(responseData); // UI更新操作
                        navigateToSingleTestActivity();
                    }
                });
                Log.e("leo", "onResponse: " + responseData);
            }
        });
    }

    private void handleFailure(String errMsg) {
        // UI更新操作，例如显示失败信息
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE); // 恢复屏幕操作
        Toast.makeText(HomeActivity.this, "请求失败:" + errMsg, Toast.LENGTH_SHORT).show();
    }

    private void handleResponse(String responseData) {
        // UI更新操作，例如显示响应数据
        Toast.makeText(HomeActivity.this, "请求成功:" + responseData, Toast.LENGTH_SHORT).show();

    }

    private void navigateToSingleTestActivity() {
        Intent intent = new Intent(HomeActivity.this, SingleTestActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
        // TODO 清理登录记录
        finish();  // 结束当前活动
    }



}